package com.imageim.yimirror.common.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;

/**
 * Created by Yuaihen.
 * on 2018/11/27
 * TipsDialog 引导页  提示蒙版
 */
public abstract class TipDialog extends Dialog {

    private Context context;
    private View tipsView;

    public TipDialog(@NonNull Context context) {
        super(context);
        this.context = context;
        hideBottomUIMenu();
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);

        // 替换dialog默认背景，否则dialog的边缘发虚透明而且很宽
        if (getWindow() != null) {
            getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        }
        tipsView = View.inflate(this.context, setLayoutId(), null);

        setContentView(tipsView);
        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

        initView(tipsView);
        initListener();
    }

    /**
     * 需要创建自己的布局
     *
     * @return 返回对应布局的ID
     */
    public abstract int setLayoutId();


    /**
     * 触摸后隐藏Dialog Dismiss Dialog
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        this.dismiss();
        return super.onTouchEvent(event);
    }

    protected abstract void initView(View tipsView);

    protected void initListener() {

    }

    /**
     * 隐藏虚拟按键，并且全屏
     */
    protected void hideBottomUIMenu() {
        // lower api
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.HONEYCOMB && Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
            View v = this.getWindow().getDecorView();
            v.setSystemUiVisibility(View.GONE);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            //for new api versions.
            View decorView = getWindow().getDecorView();
            int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_FULLSCREEN;
            decorView.setSystemUiVisibility(uiOptions);
        }
    }


}
